package com.khadi.testcase.view.paint;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import com.khadi.testcase.util.SessionUtil;

public class PaintSurface extends JComponent implements MouseListener,
		MouseMotionListener, ActionListener {
	Point startDrag, endDrag;
	List<Shape> shapes = new ArrayList<Shape>();
	JPopupMenu popupMenu = new JPopupMenu();

	public PaintSurface() {
		addMouseListener(this);
		this.add(popupMenu);
	}

	private Component getPopupMenu() {

		return null;
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		Graphics2D g2 = (Graphics2D) g;
		paintBackground(g2);

		g2.setStroke(new BasicStroke(2));
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
				0.50f));
		if (startDrag != null && endDrag != null) {
			Shape line = makeLine(startDrag.x, startDrag.y, endDrag.x,
					endDrag.y);
			g2.setPaint(Color.BLACK);
			g2.draw(line);
		}
	}

	private void paintBackground(Graphics2D g2) {
		g2.setPaint(Color.LIGHT_GRAY);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		for (int i = 0; i < getSize().width; i += 12) {
			Shape line = new Line2D.Float(i, 0, i, getSize().height);
			g2.draw(line);
		}

		for (int i = 0; i < getSize().height; i += 12) {
			Shape line = new Line2D.Float(0, i, getSize().width, i);
			g2.draw(line);
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	private void addPopupMenuItems() {
		JMenu addWorkFlow = new JMenu("Add Workflow");
		addWorkFlow(addWorkFlow);
		// addWorkFlow.add(new JMenuItem("Login work flow"));

		popupMenu.add(addWorkFlow);
		JMenuItem linkWorkFlow = new JMenuItem("Link Work flow");
		popupMenu.add(linkWorkFlow);
	}

	private void addWorkFlow(JMenu addWorkFlow) {
		Map<String, Object> workFlowMap = (Map<String, Object>) SessionUtil
				.getInstance().getSessionMap().get(SessionUtil.WORK_FLOW);
		Set<String> keySet = workFlowMap.keySet();
		for (String workFlowName : keySet) {
			addWorkFlow.add(new JMenuItem(workFlowName));
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		startDrag = new Point(e.getX(), e.getY());
		endDrag = startDrag;
		repaint();
		if (e.isPopupTrigger()) {
			if (popupMenu.getComponentCount() == 0) {
				addPopupMenuItems();
			}
			popupMenu.show(e.getComponent(), e.getPoint().x, e.getPoint().y);
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		endDrag = new Point(e.getX(), e.getY());
		repaint();
		if (e.isPopupTrigger()) {
			if (popupMenu.getComponentCount() == 0) {
				addPopupMenuItems();
			}
			popupMenu.show(e.getComponent(), e.getPoint().x, e.getPoint().y);
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		endDrag = new Point(e.getX(), e.getY());
		repaint();

	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
		return new Line2D.Float(x1, y1, x2, y2);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}
}
